package labox.innovation.gameserver.script.faenor;

import java.util.List;
import java.util.Map;

import javax.script.ScriptContext;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.gameserver.Announcements;
import labox.innovation.gameserver.datatables.EventDroplist;
import labox.innovation.gameserver.model.FuzeDropCategory;
import labox.innovation.gameserver.model.FuzeDropData;
import labox.innovation.gameserver.model.L2PetData;
import labox.innovation.gameserver.script.DateRange;
import labox.innovation.gameserver.script.EngineInterface;
import labox.innovation.gameserver.script.Expression;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * @author Luis Arias
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FaenorInterface implements EngineInterface {
	protected static final Logger _log = LoggerFactory.getLogger(FaenorInterface.class.getName());

	public static FaenorInterface getInstance() {
		return SingletonHolder._instance;
	}

	private FaenorInterface() {
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.script.EngineInterface#getAllPlayers()
	 */
	public List<?> getAllPlayers() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *
	 * Adds a new Quest Drop to an NPC
	 *
	 * @see labox.innovation.gameserver.script.EngineInterface#addQuestDrop(int)
	 */
	public void addQuestDrop(int npcID, int itemID, int min, int max, int chance, String questID, String[] states) {
		NpcTemplate npc = npcTable.getTemplate(npcID);
		if (npc == null) {
			throw new NullPointerException();
		}
		FuzeDropData drop = new FuzeDropData();
		drop.setItemId(itemID);
//		drop.setMinDrop(min);
//		drop.setMaxDrop(max);
		drop.setChance(chance);
		drop.setQuestID(questID);
		drop.addStates(states);
		addDrop(npc, drop, false);
	}

	/**
	 *
	 * Adds a new Drop to an NPC
	 *
	 * @see labox.innovation.gameserver.script.EngineInterface#addQuestDrop(int)
	 */
	public void addDrop(int npcID, int itemID, int min, int max, boolean sweep, int chance) throws NullPointerException {
		NpcTemplate npc = npcTable.getTemplate(npcID);
		if (npc == null) {
			if (_log.isDebugEnabled())
				_log.warn("Npc doesnt Exist", Config.SERVER_ID, "�ű�����");
			throw new NullPointerException();
		}
		FuzeDropData drop = new FuzeDropData();
		drop.setItemId(itemID);
//		drop.setMinDrop(min);
//		drop.setMaxDrop(max);
		drop.setChance(chance);

		addDrop(npc, drop, sweep);
	}

	/**
	 * Adds a new drop to an NPC.  If the drop is sweep, it adds it to the NPC's Sweep category
	 * If the drop is non-sweep, it creates a new category for this drop.
	 *
	 * @param npc
	 * @param drop
	 * @param sweep
	 */
	public void addDrop(NpcTemplate npc, FuzeDropData drop, boolean sweep) {
		if (sweep)
			addDrop(npc, drop, -1);
		else {
			int maxCategory = -1;

//			if (npc.getDropData() != null)
//				for (FuzeDropCategory cat : npc.getDropData()) {
//					if (maxCategory < cat.getCategoryType())
//						maxCategory = cat.getCategoryType();
//				}
			maxCategory++;
//			npc.addDropData(drop, maxCategory);
		}

	}

	/**
	 * Adds a new drop to an NPC, in the specified category.  If the category does not exist,
	 * it is created.
	 *
	 * @param npc
	 * @param drop
	 * @param sweep
	 */
	public void addDrop(NpcTemplate npc, FuzeDropData drop, int category) {
//		npc.addDropData(drop, category);
	}

	/**
	 * @return Returns the _questDrops.
	 */
	public List<FuzeDropData> getQuestDrops(int npcID) {
		NpcTemplate npc = npcTable.getTemplate(npcID);
		if (npc == null) {
			return null;
		}
		List<FuzeDropData> questDrops = new FastList<FuzeDropData>();
		if (npc.getDropData() != null)
			for (FuzeDropCategory cat : npc.getDropData())
				for (FuzeDropData drop : cat.getAllDrops()) {
					if (drop.getQuestID() != null) {
						questDrops.add(drop);
					}
				}
		return questDrops;
	}

	public void addEventDrop(int[] items, int[] count, double chance, DateRange range) {
		EventDroplist.getInstance().addGlobalDrop(items, count, (int) (chance * FuzeDropData.MAX_CHANCE), range);
	}

	public void onPlayerLogin(String[] message, DateRange validDateRange) {
//		Announcements.getInstance().addEventAnnouncement(validDateRange, message,0);
	}

	public void addPetData(ScriptContext context, int petID, int levelStart, int levelEnd, Map<String, String> stats) throws ScriptException {
		L2PetData[] petData = new L2PetData[levelEnd - levelStart + 1];
		int value = 0;
		for (int level = levelStart; level <= levelEnd; level++) {
			petData[level - 1] = new L2PetData();
			petData[level - 1].setPetID(petID);
			petData[level - 1].setPetLevel(level);

			context.setAttribute("level", new Double(level), ScriptContext.ENGINE_SCOPE);
			for (String stat : stats.keySet()) {
				value = ((Number) Expression.eval(context, "beanshell", stats.get(stat))).intValue();
				petData[level - 1].setStat(stat, value);
			}
			context.removeAttribute("level", ScriptContext.ENGINE_SCOPE);
		}

	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final FaenorInterface _instance = new FaenorInterface();
	}
}
